package com.qf.controller;
/*
 *@Description:
 *@author:刘哥
 *@Date:15:41 2022/8/13
 */

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * projectName:xiaomi
 * author:刘哥
 * time:2022/8/13 15:41
 * description:
 */
public class BaseServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       request.setCharacterEncoding("utf-8");
        String methodName = request.getParameter("method");
        try {
            Method method = this.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            if (method != null) {
                String url = (String) method.invoke(this, request, response);
                if (url == null || url.length()==0) {
                    return;
                }
                if (url.startsWith("redirect")) {
                    String realUrl = url.substring(url.indexOf(":")+1);
                    response.sendRedirect(request.getContextPath()+realUrl);
                }else{
                    request.getRequestDispatcher(url).forward(request,response);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
